package com.dexilaonian.adpter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dexilaonian.R;
import com.dexilaonian.activity.NewAddressActivity;
import com.dexilaonian.bean.MangerAddressBean;
import com.dexilaonian.bean.PspecBean;
import com.dexilaonian.bean.PspecBean2;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.Logutil;
import com.dexilaonian.utils.Okhttp;
import com.dexilaonian.view.LoadingDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class MangerAddressAdapter extends RecyclerView.Adapter<MangerAddressAdapter.ViewHolder>{

    private final List<PspecBean> mList;
    private final ArrayList<PspecBean2> mSerInfo;
    private  Context mContext;
    private  List<MangerAddressBean> mMangerList;
    private boolean isTrue =true;
    private int weizhi;
    private MyItemClickListener myItemClickListener;
    private int POSITION;
    private String mConfirms;

    public void setChacePosition(int position) {
        POSITION = position;
    }

    public MangerAddressAdapter(Context context, List<MangerAddressBean> mangerList, List<PspecBean> list, String confirms, ArrayList<PspecBean2> serInfo) {
        mContext = context;
        mMangerList = mangerList;
        mList = list;
        mConfirms = confirms;
        mSerInfo = serInfo;
        Log.e("TTTLLL", mangerList + "=" + list);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.manger_item, null);
        ViewHolder viewHolder = new ViewHolder(view,myItemClickListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final String manger_id = mMangerList.get(position).getId();
        final String receiverAddress = mMangerList.get(position).getReceiverAddress();
        final String receiverCity = mMangerList.get(position).getReceiverCity();
        final String receiverDistrict = mMangerList.get(position).getReceiverDistrict();
        final String receiverName = mMangerList.get(position).getReceiverName();
        final String receiverPhone = mMangerList.get(position).getReceiverPhone();
        final String receiverProvince = mMangerList.get(position).getReceiverProvince();
        final String receiverZIP = mMangerList.get(position).getReceiverZIP();
        final boolean isDefault = mMangerList.get(position).getIsDefault();
        if ("true".equals(isDefault+"")) {
            holder.item_moren.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.queren));
            weizhi = 0;
        } else {
            holder.item_moren.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.weiqueren));
            weizhi = 1;
        }
        if (POSITION == position) {
            holder.item_moren.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.queren));
        } else {
            holder.item_moren.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.weiqueren));
        }
       /* holder.item_moren.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (weizhi == 0) {
                    holder.item_moren.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.weiqueren));
                    weizhi = 1;
                } else {
                    holder.item_moren.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.queren));
                    weizhi = 0;
                }
            }
        });*/

        if (!TextUtils.isEmpty(receiverAddress)) {
            holder.mangerAddress.setText("收货地址:"+receiverAddress+" "+receiverProvince+receiverCity+receiverDistrict);
        }
     /*   if (!TextUtils.isEmpty(receiverCity)||!TextUtils.isEmpty(receiverProvince)||!TextUtils.isEmpty(receiverDistrict)) {
            holder.mangerAddress.setText(receiverProvince+" "+receiverCity+" "+receiverDistrict);
        }*/
        if (!TextUtils.isEmpty(receiverName)) {
            holder.mangerName.setText("收货人:"+receiverName);
        }
        if (!TextUtils.isEmpty(receiverPhone)) {
            holder.mangerPhone.setText(receiverPhone);
        }
        holder.mangerEdit.setOnClickListener(new View.OnClickListener() {
            private int quantity;
            private String pspec;
            private int productId;
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, NewAddressActivity.class);
                Log.e("TTTTTTTMCON", mConfirms+"==="+mSerInfo+"="+mList.size());
                if (mList.size() !=0 ) {
                    productId = mList.get(0).getProductId();
                    pspec = mList.get(0).getPspec();
                    quantity = mList.get(0).getQuantity();
                }
              if (mConfirms.equals("CONFIRMS")) {
                        intent.putExtra("productId", productId);
                        intent.putExtra("quantity", quantity);
                        intent.putExtra("pspec", pspec);
                        intent.putExtra("CONFIRMS", "CONFIRMS");
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("PSPS",mSerInfo);
                        intent.putExtras(bundle);
                        intent.putExtra("CONFIRMS", "CONFIRMS1");
                }
                intent.putExtra("NUM", "NN");
                intent.putExtra("addressid", manger_id);
                intent.putExtra("mangerName", receiverName);
                intent.putExtra("receiverAddress", receiverAddress);
                intent.putExtra("receiverCity", receiverCity);
                intent.putExtra("receiverProvince", receiverProvince);
                intent.putExtra("receiverDistrict", receiverDistrict);
                intent.putExtra("receiverPhone", receiverPhone);
                intent.putExtra("receiverZIP", receiverZIP);//邮政编码
                intent.putExtra("isDefault", isDefault+"");
                mContext.startActivity(intent);
                if (Activity.class.isInstance(mContext)) {
                    // 转化为activity，然后finish就行了
                    Activity activity = (Activity) mContext;
                    activity.finish();
                }
            }
        });
        final LoadingDialog dialog = new LoadingDialog(mContext);

        holder.mangerDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show();
                String delete_url = Constant.base_url + "/shipping/remove.do";
                Map<String, String> delete_map = new HashMap<String, String>();
                delete_map.put("addressId", manger_id);
                Okhttp.postString(true, delete_url, delete_map, new Okhttp.CallBac() {
                    @Override
                    public void onError(Call call, Exception e, String state, int id) {
                        dialog.cancel();
                        Toast.makeText(mContext,state,Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        dialog.cancel();
                        mMangerList.remove(mMangerList.get(position));
                        notifyDataSetChanged();
                    }

                    @Override
                    public void onNoNetwork(String state) {
                        dialog.cancel();
                        Toast.makeText(mContext,state,Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

    }
    @Override
    public int getItemCount() {
        return mMangerList == null ? 0 :mMangerList.size();

    }
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private final TextView mangerName;
        private final TextView mangerAddress;
        private final TextView mangerPhone;
        private final TextView mangerEdit;
        private final TextView mangerDel;
        public ImageView item_moren;
        private MyItemClickListener mListener;
        public ViewHolder(View itemView, MyItemClickListener myItemClickListener) {
            super(itemView);
            mangerName = (TextView) itemView.findViewById(R.id.manger_name);
            mangerAddress = (TextView) itemView.findViewById(R.id.manger_address);
            mangerPhone = (TextView) itemView.findViewById(R.id.manger_phone);
            mangerEdit = (TextView) itemView.findViewById(R.id.manger_edit);
            mangerDel = (TextView) itemView.findViewById(R.id.manger_del);
            item_moren = (ImageView) itemView.findViewById(R.id.iv_item_moren);
            //将全局的监听赋值给接口
            this.mListener = myItemClickListener;
            itemView.setOnClickListener(this);
        }



        /**
         * 实现OnClickListener接口重写的方法
         * @param v
         */
        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClick(v, getPosition());
            }

        }
    }
    public interface MyItemClickListener{
        void onItemClick(View view,int position);
    }

    public void setItemClickListener(MyItemClickListener myItemClickListener) {
        this.myItemClickListener =myItemClickListener;
    }
}
